<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ArticleController extends Controller
{
    public function add(Request $request)
    {
        $title = $request->input('title');
        $content = $request->input('content');

//        $r = DB::insert('insert into article (title,content) values(?,?)', [$title, $content]);

        $r=DB::table('article')->insert(
            [
                'title'=>$title,
                'content'=>$content
            ]
        );

        if ($r) {
            return 'success';
        }
        return 'fail';
    }

    public function del($id)
    {
//        $r = DB::delete('delete from article where id=?', [$id]);

        $r=DB::table('article')
            ->where('id',$id)
            ->delete();

        if ($r) {
            return 'success';
        } else {
            return 'fail';
        }
    }

    public function update(Request $request, $id)
    {
        $title = $request->input('title');
        $content = $request->input('content');
//        $r = DB::update('update article set title=?,content=? where id=?', [$title, $content, $id]);

        $r=DB::table('article')
            ->where('id',$id)
            ->update(
                [
                    'title'=>$title,
                    'content'=>$content
                ]
            );

        if ($r) {
            return 'success';
        } else {
            return 'fail';
        }
    }

    public function getListByCate($cateid = null)
    {
        if ($cateid != null) {
//            $r = DB::select('select * from article where cateid=? limit 10', [$cateid]);
            $r=DB::table('article')
                ->orderBy('id','desc')
                ->offset(0)
                ->limit(10)
                ->get();
        } else {
//            $r = DB::select('select * from article limit 10');
            $r=DB::table('article')
                ->orderBy('id','desc')
                ->limit(10)
                ->get();
        }
        if ($r) {
            return $r;
        } else {
            return '[]';
        }
    }

    public function show($id)
    {
//        $r = DB::select('select * from article where id=?', [$id]);
        $r=DB::table('article')
            ->where('id',$id)
            ->first();
        return $r;
    }

    public function colseComment($id)
    {
        $r=DB::table('article')
            ->where('id',$id)
            ->update(
                [
                    'is_allowcomment'=>0
                ]
            );
        if ($r) {
            return 'success';
        } else {
            return 'fail';
        }
    }

    public function openComment($id)
    {
//        $r = DB::update('update article set is_allowcomnent=1 where id=?', [$id]);
        $r=DB::table('article')
            ->where('id',$id)
            ->update(
                [
                    'is_allowcomment'=>0
                ]
            );
        if ($r) {
            return 'success';
        } else {
            return 'fail';
        }
    }
}